{include "INC_header.html" title="SSO/授权/配置"}
<div id="app-outlet" class="app-outlet">
    <!-- ant-design 应用需要配置 -->
    <a-config-provider :locale="configs.locale" :theme="configs.theme">
        <div class="app-container">
            <main>
                <div class="ui form padding">
                    <div class="field inline">
                        <div class="label">短信验证授权是否自动注册</div>
                        <div class="inner">
                            <a-switch v-model:checked="config.smsAutoRegister" checked-children="是" un-checked-children="否" />
                        </div>
                    </div>
                    <div class="field inline">
                        <div class="label">第三方平台授权是否自动注册</div>
                        <div class="inner">
                            <a-switch v-model:checked="config.thirdPartyAutoRegister" checked-children="是" un-checked-children="否" />
                        </div>
                    </div>
                    <div class="field">
                        <div class="label">授权Token有效时长(秒)，0是无限</div>
                        <div class="">
                            <a-input-number :min="0" v-model:value="config.tokenLifeTime"></a-input-number>
                        </div>
                    </div>
                </div>
            </main>
            <footer class="light-background border-top">
                <div class="ui flex-item padding">
                    <div class="center"></div>
                    <div class="end">
                        <a-button type="primary" @click="save()"><i class="fa-regular fa-floppy-disk margin-right-half"></i> 保存设置</a-button>
                    </div>
                </div>
            </footer>
        </div>
    </a-config-provider>
</div>
<script>
    //vue app
    const { createApp } = Vue;
    const { message } = antd;
    const app = createApp({
        data() {
            return {
                configs: window.appProviderConfig,
                //数据
                config: {
                    smsAutoRegister: true,
                    thirdPartyAutoRegister: true,
                    tokenLifeTime: 0,
                },
                saveLoading: false
            }
        },
        mounted() {
            //挂载成功
            const hide = message.loading("正在加载配置...");
            let self = this;
            axios.get("{url url='/sso/api/admin/auth/config'}").then(({ data }) => {
                hide();
                if (data.error === 0) {
                    self.config = data.data;
                } else {
                    message.error(data.message);
                }
            }).catch(() => {
                hide();
                message.error("服务端繁忙");
            });
        },
        methods: {
            //方法

            save: function () {
                if (this.saveLoading) {
                    return;
                }
                this.saveLoading = true;
                let self = this;
                axios.post("{url url='/sso/api/admin/auth/saveConfig'}", {
                    smsAutoRegister: this.config.smsAutoRegister ? 1 : 0,
                    thirdPartyAutoRegister: this.config.thirdPartyAutoRegister ? 1 : 0,
                    tokenLifeTime: this.config.tokenLifeTime

                }).then(({ data }) => {
                    self.saveLoading = false;
                    if (data.error === 0) {
                        message.success(data.message);
                    } else {
                        message.error(data.message);
                    }
                }).catch(() => {
                    self.saveLoading = false;
                    message.error("服务端繁忙");
                });
            }
        }
    }).use(antd);
    //挂载
    const vm = app.mount("#app-outlet");
    //
    if (window.parent.length > 0) {
        window.parent.postMessage({
            event: "location",
            data: [
                { name:"授权" },
                { name:"配置" }
            ]
        });
    }
</script>
{include "INC_footer.html"}